<template>
    <view>
        <view style="text-align: center; padding: 20rpx 0">{{ hour }}:{{ minute }}</view>
        <picker-view indicator-style="height: 50px;" style="width: 100%; height: 100vh" :value="value" @change="bindChange">
            <picker-view-column>
                <view style="line-height: 50px; text-align: center" v-for="(item, index) in hours" :key="index">{{ item }} 时</view>
            </picker-view-column>
            <picker-view-column>
                <view style="line-height: 50px; text-align: center" v-for="(item, index) in minutes" :key="index">{{ item }} 分</view>
            </picker-view-column>
        </picker-view>
    </view>
</template>

<script>
const hours = [];
const minutes = [];

for (let i = 0; i <= 23; i++) {
    hours.push(i);
}

for (let i = 0; i <= 59; i++) {
    minutes.push(i);
}

export default {
    data() {
        return {
            parseCron: getApp().globalData.utils.parseCron,
            hours,
            minutes,
            hour: '',
            minute: '',
            value: []
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        console.log('=== scene:condition-timer-time:onLoad:');
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log('=== scene:condition-timer-time:onReady:');
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('=== scene:condition-timer-time:onShow:');
        let conditionList = uni.getStorageSync('scene').conditionList; // getApp().globalData.scene.conditionList

        let fields = this.parseCron(conditionList);
        let hour = fields.hour[0];
        let minute = fields.minute[0];
        this.hour = hour
        this.minute = minute
        this.value = [hour, minute]
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        console.log('--- scene:condition-timer-time:onHide:');
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console.log('--- scene:condition-timer-time:onUnload:');
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        bindChange(ev) {
            const value = ev.detail.value;
            let hour = value[0];
            let minute = value[1]; // let scene = wx.getStorageSync('scene')

            let conditionList = uni.getStorageSync('scene').conditionList; // getApp().globalData.scene.conditionList

            let expression = '';

            for (let i = 0; i < conditionList.length; i++) {
                const item = conditionList[i];

                if (item.conditionType === 1) {
                    expression = item.conditionValue.cron;
                    let expressionArray = expression.split(' ');
                    expressionArray[1] = minute;
                    expressionArray[2] = hour;
                    let newExpression = expressionArray.join(' ');
                    item.conditionValue.cron = newExpression;
                    break;
                }
            } // scene.conditionList = conditionList
            // wx.setStorageSync('scene', scene)

            getApp().syncScene(this, {
                conditionList: conditionList
            });
            this.hour = hour
            this.minute = minute <= 9 ? '0' + minute : minute
        }
    }
};
</script>
<style></style>
